package com.ycb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycb.common.Result;
import com.ycb.pojo.Employee;
import com.ycb.mapper.EmployeeMapper;
import com.ycb.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author ycb
 * @since 2024-09-30
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Override
    public Result<Employee> login(HttpServletRequest httpServletRequest, Employee employee) {
        //1.将页面提交的密码md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据提交的username查询用户名
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("username",employee.getUsername());
        Employee emp = this.getOne(employeeQueryWrapper);

        //3.如果没有查询到，则返回登录失败结果
        if (emp == null) {
            return Result.error("登陆失败，该账号不存在");
        }

        //4.密码比对是，如果不一致则返回登陆失败
        if (!emp.getPassword().equals(password)) {
            return Result.error("登录失败，密码错误");
        }

        //5.查看员工状态，如果为已警用，则返回失败
        if (emp.getStatus().equals(0)) {
            return Result.error("该账号已经禁用");
        }
        httpServletRequest.getSession().setAttribute("employee",emp.getId());
        return Result.success(emp);
    }
}
